package zip;

import Util.CreateFileUtil;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.xerial.snappy.Snappy;
import org.xerial.snappy.SnappyFramedOutputStream;
import org.xerial.snappy.SnappyOutputStream;

import java.io.*;
import java.util.Base64;
import java.util.HashMap;

/**
 * Snappy library tests
 */
public class SnappyTest extends TestParent {
    @Param({"1", "2", "3", "4", "5", "6", "7", "8", "9"})
    public int LEVEL;
    static HashMap<Integer, byte[]> compressFile;
    static HashMap<Integer, byte[]> deCompressFile;
    static {
        compressFile = new HashMap<>();
        deCompressFile = new HashMap<>();
        for (int i = 1; i < 10; i++) {
            compressFile.put(i, CreateFileUtil.getBytesByFile("./test/test_"+i+".json"));
            deCompressFile.put(i,CreateFileUtil.getBytesByFile("./test/test_"+i+".Snappy"));
        }
    }


    @Benchmark
    public ByteArrayOutputStream snappyNormalOutput() throws IOException
    {
        return baseBenchmark(underlyingStream -> new SnappyOutputStream( underlyingStream, 65536 ));
    }
    @Benchmark
    public byte[] compress() throws IOException
    {
        byte[] out = Base64.getEncoder().encode(Snappy.compress(compressFile.get(LEVEL)));;
        CreateFileUtil.bytes2File(out,"./test/","test_"+LEVEL+".Snappy");
        return out;
    }

    @Benchmark
    public ByteArrayOutputStream snappyFramedOutput() throws IOException
    {
        return baseBenchmark(underlyingStream -> new SnappyFramedOutputStream( underlyingStream ));
    }
    @Benchmark
    public byte[] deCompress() throws IOException{
        byte[] out = Snappy.uncompress(Base64.getDecoder().decode(deCompressFile.get(LEVEL)));
        CreateFileUtil.bytes2File(out,"./test/","test_"+LEVEL+".Snappy.json");
        return out;
    }


}
